﻿using System.Reflection;
using SBlogCore.EventBus.CrossProcessEventBus.Attributes;

namespace SBlogCore.EventBus.CrossProcessEventBus.Model
{
    /// <summary>
    /// EventBus事件总线
    /// </summary>
    public class EventMember
    {
        public EventMember(MethodInfo method)
        {
            InType = method.DeclaringType!;
            Method = method;

        }
        /// <summary>
        /// 所属实例类型
        /// </summary>
        public Type InType { get; }
        /// <summary>
        /// 事件名称
        /// </summary>
        public string EventName
        {
            get
            {
                var attribute = Method.GetCustomAttribute<CrossEventHandlerAttribute>();
                var eventName = attribute?.Name;
                //如果没有用特性标注事件名称
                if (eventName == null)
                {
                    //取参数作为事件名称
                    eventName = string.Join('_', Method.GetParameters().Select(s => s.GetType().Name));
                }
                return eventName ?? throw new Exception("请为特性InEventHandlerAttribute标记事件名称，或者保证参数不能为空！");
            }
        }
        /// <summary>
        /// 方法
        /// </summary>
        public MethodInfo Method { get;}
    }
}
